package me.heyclock.hl.recordwriter.robot;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.ui.RecognizerDialogListener;

import me.heyclock.hl.recordwriter.bean.RecordeBean;

/**
 * @des 语音识别监听
 * @author  hl
 * @date 2018.06.08
 */
public class MyRecognizerDialogLis implements RecognizerDialogListener {
    private Context context;
    private RecDialCallBack recDialCallBack;
    private String audioText = "";

    public MyRecognizerDialogLis(Context context, RecDialCallBack recDialCallBack) {
        this.context = context;
        this.recDialCallBack = recDialCallBack;
    }

    @Override
    public void onResult(RecognizerResult recognizerResult, boolean isLast) {
        Log.e("test", "b=" + isLast);
        Log.e("test", "getResultString=" + recognizerResult.getResultString());
        Log.e("test", "describeContents=" + recognizerResult.describeContents());
        RecordeBean recordeBean = JSON.parseObject(recognizerResult.getResultString(), new TypeReference<RecordeBean>() {
        });
        audioText += recordeBean.getAllWs();

        ///< 成功之后临时文件拷贝过去
        if (isLast) {
            recDialCallBack.replyMsg(audioText);
            audioText = "";
        }
    }

    @Override
    public void onError(SpeechError speechError) {
    }

    public interface RecDialCallBack{
        void replyMsg(String msg);
    }
}
